package com.iot.manage.dao;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.Gateway;
import com.iot.manage.pojo.SysCode;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface GatewayMapper {

    @Select("<script>" +
            "SELECT\n" +
            "temp.GATEWAY_ID gatewayId,\n" +
            "temp.GATEWAY_CODE gatewayCode,\n" +
            "temp.GATEWAY_NAME gatewayName,\n" +
            "temp.CODE_NAME gatewayType,\n" +
            "temp.GATEWAY_ARG gatewayArg,\n" +
            "case when temp.gatewayParentCode IN (SELECT GATEWAY_CODE FROM T_GATEWAY WHERE GATEWAY_TYPE = 'gtype-gateway') then temp.gatewayParentCode else '' END gatewayParentCode\n" +
            "FROM (\n" +
            "SELECT\n" +
            "ROWNUM rowno,\n" +
            "t.*,\n" +
            "c.*,\n" +
            "a.GATEWAY_CODE gatewayParentCode\n" +
            "FROM T_GATEWAY t\n" +
            "LEFT JOIN (SELECT * FROM T_SYS_CODE WHERE CODE_TYPE='网关类型') c ON t.GATEWAY_TYPE = c.CODE_VALUE \n" +
            "LEFT JOIN T_GATEWAY a ON t.PARENT_ID= a.GATEWAY_ID \n" +
            "WHERE (t.GATEWAY_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') OR t.GATEWAY_CODE LIKE CONCAT(CONCAT('%',#{query}),'%'))\n" +
            "AND ROWNUM &lt;= #{pagesize} ORDER BY t.GATEWAY_ID\n" +
            ") temp WHERE temp.rowno &gt; #{start}\n" +
            "</script>")
    List<Gateway> findGatewayList(@Param("query") String query, @Param("start") int start, @Param("pagesize") int pagesize);

    @Select("<script>SELECT COUNT(a.GATEWAY_ID) FROM T_GATEWAY a WHERE a.GATEWAY_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') OR a.GATEWAY_CODE LIKE CONCAT(CONCAT('%', #{query}),'%')</script>")
    int countGatewayList(String query);

    @Select("<script>SELECT a.GATEWAY_ID codeValue, a.GATEWAY_CODE codeName FROM T_GATEWAY a  WHERE a.GATEWAY_TYPE = 'gtype-gateway' ORDER BY a.GATEWAY_CODE</script>")
    List<SysCode> findGatewayParentCodeList();

    @Select("<script>SELECT a.CODE_VALUE codeValue, a.CODE_NAME codeName FROM T_SYS_CODE a WHERE a.CODE_TYPE='网关类型' ORDER BY a.CODE_VALUE DESC</script>")
    List<SysCode> findGatewayTypeList();

    @Insert("<script>" +
            "INSERT INTO T_GATEWAY \n" +
            "(GATEWAY_ID,GATEWAY_CODE,GATEWAY_NAME,GATEWAY_TYPE" +
            "<if test=\" gatewayArg != null and gatewayArg != ''\">"+
            ",GATEWAY_ARG" +
            "</if>"+
            "<if test=\" gatewayParentCode != null and gatewayParentCode != '' \">"+
            ",PARENT_ID" +
            "</if>"+
            ") \n" +
            "VALUES \n" +
            "(#{gatewayId},#{gatewayCode},#{gatewayName},#{gatewayType}" +
            "<if test=\" gatewayArg != null and gatewayArg != ''\">"+
            ",#{gatewayArg}" +
            "</if>"+
            "<if test=\" gatewayParentCode != null and gatewayParentCode != ''\">"+
            ",#{gatewayParentCode}" +
            "</if>"+
            ")" +
            "</script>")
    void addGatewayInfo(JSONObject params);

    @Select("<script>" +
            "SELECT  \n" +
            " temp.GATEWAY_ID gatewayId,\n" +
            " temp.GATEWAY_CODE gatewayCode,\n" +
            " temp.GATEWAY_NAME gatewayName,\n" +
            " temp.GATEWAY_TYPE gatewayType,\n" +
            " temp.GATEWAY_ARG gatewayArg,\n" +
            " case when temp.PARENT_ID IN (SELECT GATEWAY_ID FROM T_GATEWAY WHERE GATEWAY_TYPE = 'gtype-gateway') then temp.PARENT_ID else '' END gatewayParentCode\n" +
            "FROM T_GATEWAY temp\n" +
            "\tWHERE temp.GATEWAY_ID = #{params} OR temp.GATEWAY_CODE = #{params}\n" +
            "</script>")
    Gateway findGatewayInfoByIdOrCode(String params);

    @Update("<script>" +
            "UPDATE T_GATEWAY SET \n" +
            "\tGATEWAY_CODE = #{gatewayCode},\n" +
            "\tGATEWAY_NAME = #{gatewayName},\n" +
            "\tGATEWAY_TYPE = #{gatewayType},\n" +
            "\tGATEWAY_ARG = #{gatewayArg}\n" +
            "<if test=\" gatewayParentCode != null\">"+
            "\t,PARENT_ID = #{gatewayParentCode}\n" +
            "</if>"+
            "WHERE GATEWAY_ID = #{gatewayId}" +
            "</script>")
    void updateGatewayInfo(JSONObject params);

    @Delete("<script>DELETE FROM T_GATEWAY WHERE GATEWAY_ID = #{companyId}</script>")
    void removeGatewayInfo(String companyId);

    @Select("<script>SELECT count(1) FROM T_GATEWAY WHERE PARENT_ID = #{gatewayId}</script>")
    int countGatewayParentById(String gatewayId);

    @Update("<script>UPDATE T_GATEWAY SET PARENT_ID = '' WHERE PARENT_ID = #{gatewayId}</script>")
    void updateGatewayParent(String gatewayId);
}
